﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
namespace Data
{
   public class KhachHangRepository
    {
       public static KhachHangDTO GetKhachHangByMaKhachHang(int maKhachHang)
       {
           NhaSachEntities context = new NhaSachEntities();
           var q = (from kh in context.KhachHangList
                   where kh.MaKhachHang == maKhachHang && kh.TinhTrang == 1
                   select new KhachHangDTO()
                   {
                       MaKhachHang = kh.MaKhachHang,
                       HoTen = kh.HoTen,
                       DiaChi = kh.DiaChi,
                       DienThoai = kh.DienThoai,
                       Email = kh.Email,
                       TienNo = (double)kh.TienNo,
                       TinhTrang = (int)kh.TinhTrang
                   }).SingleOrDefault();
           return q;
       }
       public static List<KhachHangDTO> GetKhachHangList()
       {
           NhaSachEntities context = new NhaSachEntities();
           var q = (from kh in context.KhachHangList
                    select new KhachHangDTO()
                    {
                        MaKhachHang = kh.MaKhachHang,
                        HoTen = kh.HoTen,
                        DiaChi = kh.DiaChi,
                        DienThoai = kh.DienThoai,
                        Email = kh.Email,
                        TienNo = (double)kh.TienNo,
                        TinhTrang = (int)kh.TinhTrang
                    }).DefaultIfEmpty();
           return q.ToList();
       }

       public static bool CheckDelete(int maKhachHang)
       {
           NhaSachEntities context = new NhaSachEntities();

           var hoaDon = (from hd in context.HoaDonList
                         where hd.MaKhachHang == maKhachHang
                         select hd).Count();
           if (hoaDon > 0)
               return false;

           var phieuThuTien = (from pt in context.PhieuThuTienList
                               where pt.MaKhachHang == maKhachHang
                               select pt).Count();
           if (phieuThuTien > 0)
               return false;

           var chiTietBaoCao = (from ct in context.ChiTietBaoCaoCongNoList
                                where ct.MaKhachHang == maKhachHang
                                select ct).Count();
           if (chiTietBaoCao > 0)
               return false;
           return true;


       }
       public static void Delete(int maKhachHang)
       {
           NhaSachEntities context = new NhaSachEntities();
           var khacHang = (from kh in context.KhachHangList
                           where kh.MaKhachHang == maKhachHang
                           select kh).FirstOrDefault();
           context.DeleteObject(khacHang);
           context.SaveChanges();

       }
       public static void Insert(KhachHangDTO khachHang)
       {
           NhaSachEntities context = new NhaSachEntities();
           KhachHang kh = new KhachHang()
           {
               MaKhachHang = khachHang.MaKhachHang,
               HoTen = khachHang.HoTen,
               DiaChi = khachHang.DiaChi,
               DienThoai = khachHang.DienThoai,
               Email = khachHang.Email,
               TienNo = (double)khachHang.TienNo,
               TinhTrang = (int)khachHang.TinhTrang
           };
           context.AddToKhachHangList(kh);
           context.SaveChanges();
       }
       public static void Update(KhachHangDTO khachHang)
       {
           NhaSachEntities context = new NhaSachEntities();
           var q = (from kh in context.KhachHangList
                    where kh.MaKhachHang == khachHang.MaKhachHang
                    select kh).FirstOrDefault();
           q.HoTen = khachHang.HoTen;
           q.DiaChi = khachHang.DiaChi;
           q.DienThoai = khachHang.DienThoai;
           q.Email = khachHang.DienThoai;
           q.TienNo = khachHang.TienNo;
           q.TinhTrang = khachHang.TinhTrang;
           context.SaveChanges();
       }
       public static int GetCurrentID()
       {
           NhaSachEntities context=new NhaSachEntities();
           Nullable<int> id = context.GetCurrentID("KhachHang").FirstOrDefault();
           return (int)id;
       }

       public static List<KhachHangDTO> Search(string hoTen)
       {
           NhaSachEntities context = new NhaSachEntities();
           var q = (from kh in context.KhachHangList
                    where kh.HoTen.Contains(hoTen)
                    select new KhachHangDTO()
                    {
                        MaKhachHang = kh.MaKhachHang,
                        HoTen = kh.HoTen,
                        DiaChi = kh.DiaChi,
                        DienThoai = kh.DienThoai,
                        Email = kh.Email,
                        TienNo = (double)kh.TienNo,
                        TinhTrang = (int)kh.TinhTrang
                    }).DefaultIfEmpty();
           return q.ToList();
       }
       public static List<KhachHangDTO> Search(string hoTen, string diaChi)
       {
           NhaSachEntities context = new NhaSachEntities();
           var q = (from kh in context.KhachHangList
                    where kh.HoTen.Contains(hoTen) && kh.DiaChi.Contains(diaChi)
                    select new KhachHangDTO()
                    {
                        MaKhachHang = kh.MaKhachHang,
                        HoTen = kh.HoTen,
                        DiaChi = kh.DiaChi,
                        DienThoai = kh.DienThoai,
                        Email = kh.Email,
                        TienNo = (double)kh.TienNo,
                        TinhTrang = (int)kh.TinhTrang
                    }).DefaultIfEmpty();
           return q.ToList();
       }
       public static List<KhachHangDTO> Search(string hoTen, string diaChi, string dienThoai)
       {
           NhaSachEntities context = new NhaSachEntities();
           var q = (from kh in context.KhachHangList
                    where kh.HoTen.Contains(hoTen) && kh.DiaChi.Contains(diaChi) && kh.DienThoai.Contains(dienThoai)
                    select new KhachHangDTO()
                    {
                        MaKhachHang = kh.MaKhachHang,
                        HoTen = kh.HoTen,
                        DiaChi = kh.DiaChi,
                        DienThoai = kh.DienThoai,
                        Email = kh.Email,
                        TienNo = (double)kh.TienNo,
                        TinhTrang = (int)kh.TinhTrang
                    }).DefaultIfEmpty();
           return q.ToList();
       }
       public static List<KhachHangDTO> Search(string hoTen, string diaChi, string dienThoai, string email)
       {
           NhaSachEntities context = new NhaSachEntities();
           var q = (from kh in context.KhachHangList
                    where kh.HoTen.Contains(hoTen) 
                    && kh.DiaChi.Contains(diaChi) 
                    && kh.DienThoai.Contains(dienThoai)
                    && kh.Email.Contains(email)
                    select new KhachHangDTO()
                    {
                        MaKhachHang = kh.MaKhachHang,
                        HoTen = kh.HoTen,
                        DiaChi = kh.DiaChi,
                        DienThoai = kh.DienThoai,
                        Email = kh.Email,
                        TienNo = (double)kh.TienNo,
                        TinhTrang = (int)kh.TinhTrang
                    }).DefaultIfEmpty();
           return q.ToList();
       }
    }
}
